package fictional.GUI;

import java.awt.Image;
import java.net.URL;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.SwingWorker;

/**
 * A thread which draws images in the GUI asynchronously. 
 */
public class ImageLoader extends SwingWorker<Void, Void> {

	private List<DrawData> data;
	
	protected ImageLoader(List<DrawData> data) {
		this.data = data;
	}
	
	@Override
	public Void doInBackground() {
	
		for (DrawData dataItem : data) {	
			
			URL imgURL = dataItem.getImageURL();
			
			ImageIcon imageIcon;
			if (imgURL != null) {
				imageIcon = new ImageIcon(imgURL, "image");
			} else {
				System.err.println("Couldn't find file: " + imgURL);
				return null;
			}
			
			Image image = imageIcon.getImage();
			dataItem.getLabel().setIcon(new ImageIcon(image.getScaledInstance(dataItem.getHeight(), dataItem.getWidth(), 0)));
		}
		
		data.clear();
		return null;
	}		    	
}